﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITM495.OOExamples.Core.Logger;

namespace ITM495.OOExamples.Core.Exceptions
{
    public class ExceptionsController: IController
    {
        private ILogger logger = LogManager.GetLogger();

        #region IController Members

        public void Run(Options options)
        {
            logger.Log("In Exception Logger\n");

            CatchAllException();
        }


        private void CatchAllException()
        {
            try
            {
                CatchApplicationExceptions();
            }
            catch (Exception ex)
            {
                logger.Log("----Catch All Exceptions:{0}-{1}\n", ex.GetType().Name, ex.Message);
            }
            finally
            {
                logger.Log("-----In finally, so I can clean up.\n");
            }
        }

        private void CatchApplicationExceptions()
        {
            try
            {
                CatchCustomException();
            }
            catch(NullReferenceException nullEx)
            {
                logger.Log("nullReferenceException.");
            }
            catch(ApplicationException ex)
            {
                logger.Log("---Catch Application Exception:{0} with value{1}\n", ex.GetType().Name, ex.Message);
                throw ex;
            }
        }

        private void CatchCustomException()
        {
            try
            {
                ThrowCustomException();
            }
            catch (CustomException ex)
            {
                logger.Log("--Catch Custom Exception:{0}-{1}\n", ex.GetType().Name,ex.CustomProperty);
                throw ex;
            }
        }

        private void ThrowCustomException()
        {
            CustomException ex = null;
            logger.Log(ex.CustomProperty);
            logger.Log("-In ThrowCustomException\n");
            throw new CustomException("CustomValue");
        }

        #endregion
    }
}
